function [value,pos] = findnear(A,b)
% In the array A find the element with value closest to the value b
% Syntax [value,pos] = findnear(A,b)

% dist = abs(A-b);
% W = find(dist == repmat(min(dist),size(dist)));	% The minimum of the distance vector
% if(length(W) > 1)
%     u = size(A);
%     if(u(1) > 1)
%         pos(1) = W(1);
%         pos(2) = 1;
%     else
%         pos(1) = 1;
%         pos(2) = W(1);
%     end;
% 
% else
%     [pos(1),pos(2)] = find(dist == repmat(min(dist),size(dist)));
% end;
% value = A(pos(1),pos(2));

dist = abs(A-b);
[x pos] = min(dist);
value = A(pos);